﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using HNQXYJ.Model;

namespace HNQXYJ.DAL
{
    public  class SysConfinDAL
    {

        /// <summary>
        /// 将一行数据转化为一个对象
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private SysConfig ToModel(DataRow dr)
        {
            SysConfig sc = new SysConfig();
            sc.Ip = dr["Ip"].ToString();
            sc.Port = Convert.ToInt32(dr["Port"]);
            sc.AddDate = Convert.ToDateTime(dr["AddDate"]);
            return sc;
        }


        /// <summary>
        /// 返回系统配置表（只有一行）数据的对象
        /// </summary>
        /// <returns></returns>
        public SysConfig ReturnSysCofing()
        {
            SysConfig sc = null;
            string procname = "SysConfigGetData";
            DataTable dt = SqlHelper.GetDataTable(procname);

            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                sc = ToModel(dr);

            }
            return sc;
        }

        /// <summary>
        /// 更新系统配置变（主要是ip和port）
        /// </summary>
        /// <param name="sc"></param>
        /// <returns></returns>
        public bool UpdateSysConfig(SysConfig sc)
        {
            string procName = "SysConfigUpdateData";
            SqlParameter[] paras = { 
                                      new  SqlParameter("@Ip",SqlDbType.VarChar,50),
                                      new SqlParameter("@Port",SqlDbType.Int)
                                   };
            paras[0].Value = sc.Ip;
            paras[1].Value = sc.Port;

            int res = SqlHelper.ExecNonQuery(procName, paras);
            if (res > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


    }
}